﻿using Microsoft.VisualBasic;
using SanJ.FreeCare.ValueSets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Services;
using Volo.Abp.Guids;

namespace SanJ.FreeCare.Patients
{
    public class PatientManager : DomainService
    {
        private readonly IPatientRepository _patientRepository;

        public PatientManager(IPatientRepository patientRepository)
        {
            _patientRepository = patientRepository;
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="patient"></param>
        /// <returns></returns>
        public async Task<Patient> CreateAsync(Patient patient)
        {
            if (patient.IsVaild == string.Empty)
            {
                patient.IsVaild = VaildType.Vaild;
            }

            return await _patientRepository.InsertAsync(patient);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="patient"></param>
        /// <returns></returns>
        public async Task<Patient> UpdateAsync(Patient patient)
        {
            return await _patientRepository.UpdateAsync(patient);
        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="patients"></param>
        /// <returns></returns>
        public Task UpdateBatchAsync(List<Patient> patients)
        {
            return _patientRepository.UpdateManyAsync(patients);
        }

        /// <summary>
        /// 通过PatientId查询患者信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Patient> GetByIdAsync(Guid? patientId)
        {
            return (await _patientRepository.GetListAsync(p => p.Id == patientId && p.IsVaild == VaildType.Vaild)).FirstOrDefault();
        }

        /// <summary>
        /// 通过第三方绑定ID查询(Associated)
        /// </summary>
        /// <param name="associatedId"></param>
        /// <returns></returns>
        public async Task<Patient> GetByAssociatedIdAsync(string thirdId)
        {
            return (await _patientRepository.GetListAsync(p => p.ThirdId == thirdId && p.IsVaild == VaildType.Vaild)).FirstOrDefault();
        }

        public async Task<List<Patient>> GetListByIdAsync(List<Guid> patientId)
        {
            return await _patientRepository.GetListAsync(t => patientId.Contains(t.Id));
        }

        /// <summary>
        /// 通过ID作废
        /// </summary>
        /// <param name="patientId"></param>
        /// <returns></returns>
        public async Task<Patient> CancelByIdAsync(Guid? patientId)
        {
            var patient = await GetByIdAsync(patientId);

            if (patient == null)
            {
                return new Patient();
            }

            patient.IsVaild = VaildType.InVaild;
            return await _patientRepository.UpdateAsync(patient);
        }

        /// <summary>
        /// 通过第三方绑定ID作废(AssociatedId)
        /// </summary>
        /// <param name="patientId"></param>
        /// <returns></returns>
        public async Task<Patient> CancelByAssociatedIdAsync(string associatedId)
        {
            var patient = await GetByAssociatedIdAsync(associatedId);

            if (patient == null)
            {
                throw new UserFriendlyException("未查询到患者信息！");
            }

            patient.IsVaild = VaildType.InVaild;
            return await _patientRepository.UpdateAsync(patient);
        }

    }
}
